﻿namespace WebinatorTest.IntegrationTests
{
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using BaseTests;
    using NUnit.Framework;
    using Webinator;
    using Webinator.Enums;

    [TestFixture]
    [Category("nunit")]
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
    public class FormElementsTests : WebDriverBaseTest
    {
        [SetUp]
        public void SetUp()
        {
            Open("Home.html");
        }

        [Test]
        public void VerifyAppendText()
        {
            Locator textField = new Locator(FindBy.Id, "item1");

            Web.SendKeys(textField, "world hello");
            Web.AppendText(textField, "blah");

            string value = Web.GetAttribute(textField, "value");
            Assert.AreEqual("world helloblah", value);
        }

        [Test]
        public void VerifyClear()
        {
            Locator textField = new Locator(FindBy.Id, "item1");

            Web.SendKeys(textField, "world hello");
            Web.Clear(textField);

            string value = Web.GetAttribute(textField, "value");
            Assert.AreEqual(string.Empty, value);
        }

        [Test]
        public void VerifyMultiSelectComboByAll()
        {
            Locator multiselect = LocateBy.Id("multiselect");
            Web.SelectListItem(multiselect,
                SelectBy.Text("Option 1"),
                SelectBy.PartialText("2"),
                SelectBy.Value("o3"),
                SelectBy.PartialValue("4"),
                SelectBy.Index(4));
            var selectedItems = Web.GetSelectedListItems(multiselect);

            Assert.AreEqual(5, selectedItems.Count());
        }

        [Test]
        public void VerifySelectComboByIndex()
        {
            Locator fruit = LocateBy.Id("fruitList");
            Web.SelectListItem(fruit, SelectBy.Index(2));
            var selectedIndex = Web.GetSelectedListItem(fruit).Index;

            Assert.AreEqual(2, selectedIndex);
        }

        [Test]
        public void VerifySelectComboByPartialText()
        {
            Locator fruit = LocateBy.Id("fruitList");
            Web.SelectListItem(fruit, SelectBy.PartialText("Pine"));
            string selectedValue = Web.GetSelectedListItem(fruit).Value;

            Assert.AreEqual("pineapple", selectedValue);
        }

        [Test]
        public void VerifySelectComboByPartialValue()
        {
            Locator fruit = LocateBy.Id("fruitList");
            Web.SelectListItem(fruit, SelectBy.PartialValue("pine"));
            string selectedValue = Web.GetSelectedListItem(fruit).Value;

            Assert.AreEqual("pineapple", selectedValue);
        }

        [Test]
        public void VerifySelectComboByText()
        {
            Locator fruit = LocateBy.Id("fruitList");
            Web.SelectListItem(fruit, SelectBy.Text("Pineapple"));
            string selectedValue = Web.GetSelectedListItem(fruit).Value;

            Assert.AreEqual("pineapple", selectedValue);
        }

        [Test]
        public void VerifySelectComboByValue()
        {
            Locator fruit = LocateBy.Id("fruitList");
            Web.SelectListItem(fruit, SelectBy.Value("pineapple"));
            string selectedValue = Web.GetSelectedListItem(fruit).Value;

            Assert.AreEqual("pineapple", selectedValue);
        }

        [Test]
        public void VerifySendKeys()
        {
            Locator textField = new Locator(FindBy.Id, "item1");

            Web.SendKeys(textField, "world hello");

            string value = Web.GetAttribute(textField, "value");
            Assert.AreEqual("world hello", value);
        }

        [Test]
        public void VerifySetText()
        {
            Locator textField = new Locator(FindBy.Id, "item1");

            Web.Clear(textField);
            Web.AppendText(textField, "setting text");
            
            string value = Web.GetAttribute(textField, "value");
            Assert.AreEqual("setting text", value);
        }
    }
}